package com.c.ctgproject.ui.vehiclefiles

import android.os.Bundle
import android.text.TextUtils
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.mode.Recheck
import com.c.ctgproject.retrofit.Response
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.dialog.LoadingDialog
import com.jaeger.library.StatusBarUtil
import kotlinx.android.synthetic.main.activity_vehiclefiles.*
import kotlinx.android.synthetic.main.title.*
import java.util.*

/**
 * 扫二维码进入页面 车辆二验页面
 */
class VehiclefilesActivity : AppCompatActivity(), View.OnClickListener {
    private val loading: LoadingDialog by lazy { LoadingDialog(this) }
    private val statusColor: StatusColor = StatusColor()
    private var carPlate: String? = null//车牌号
    private var carCode: String? = null//车辆编号
    private val viewModel: VehiclefilesViewModel by lazy {
        ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(application)).get(VehiclefilesViewModel::class.java)
    }
    private var dialogUtils: DialogUtils? = null

    private var token: String? = null
    private var showVideoPhoto = true
    private var carSignstr = "2"//车辆标记 1-问题车辆 2-正常车辆
    private var drivingLicense = "0"//行驶证 1-有 0-无
    private var registration = "0"//登记证 1-有 0-无
    private var idCardCopy = "0"//身份证复印件 1-有 0-无
    private var notice = "0"//告知书 1-有 0-无
    private var attorneyPaper = "0"//委托书 1-有 0-无
    private var plateApplicationForm = "0"//机动车申领表 1-有 0-无
    private var hasCheck = "0"//是否初验 1-是 0-否
    private var hasRecheck = "0"//是否二验 1-是 0-否
    private var detachable = "1"//车辆是否可拆 1-是 0-否
    private var carId: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vehiclefiles)
        StatusBarUtil.setTranslucent(this)
        statusColor.setAndroidNativeLightStatusBar(this, false)
        carCode = intent.getStringExtra("carCode")
        token = Utils.getShared2(this, "token")
        setSupportActionBar(toolbar)
        supportActionBar?.setHomeButtonEnabled(true)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        event()
        result()
    }


    private fun event() {


        confirm_button.setOnClickListener(this)

        dialogUtils = DialogUtils()
        //获取
        viewModel.recheck(token!!, carCode!!)
        viewModel.getresponsedata().observe(this, Observer { response: Response<Recheck> ->
            MyLog.d("车辆二检页面回调:${response}")
            if (response.status == 0) {
                carId = response.data.carId
                carOwner.text = response.data.carOwner
                toolbar_title.text = response.data.ownerPlate
                checkRemark.setText(response.data.checkRemark)
                recheckRemark.setText(response.data.recheckRemark)
                carSign.isChecked = response.data.carSign == "1"
                CK_1.isChecked = response.data.drivingLicense == "1"
                CK_2.isChecked = response.data.registration == "1"
                CK_3.isChecked = response.data.idCardCopy == "1"
                CK_4.isChecked = response.data.notice == "1"
                CK_5.isChecked = response.data.attorneyPaper == "1"
                CK_6.isChecked = response.data.plateApplicationForm == "1"
                radio_hasCheck.isChecked = response.data.hasCheck == "1"
                radio_hasRecheck.isChecked = response.data.hasRecheck == "1"
                //车辆是否可拆
                val chai = response.data.detachable == "0"
                if (chai){
                    mCantDetachableCB.performClick()
                }else{
                    mCanDetachableCB.performClick()
                }



                //是否二验完毕
                val eryan = response.data.hasRecheck == "1"
                if (eryan) {
                    recheckRemark.isEnabled = false
                    mCanDetachableCB.isEnabled=false
                    mCantDetachableCB.isEnabled=false
                }


                if (response.data.hasRecheck == "1") {
                    radio_hasRecheck.isClickable = false
                }

                carPlate = response.data.ownerPlate
            } else {
                Toast.makeText(this, response.msg, Toast.LENGTH_LONG).show()
                finish()
            }
        })

        viewModel.getEntranceCardata().observe(this, Observer { response: Response<*> ->
            loading.dismiss()
            if (response.status == 0) {
                showToast(response.msg)
                finish()
            } else {
                dialogUtils!!.showTwo(this@VehiclefilesActivity, "提示", response.msg)
            }
        })


        viewModel.error.observe(this, Observer {
            loading.dismiss()
            MyLog.d("通用错误回调:$it")
            CustomToast.showToast(this, it)
        })

        carSign.setOnCheckedChangeListener { _, isChecked -> carSignstr = if (isChecked) "1" else "2" }


        CK_1.setOnCheckedChangeListener { _, isChecked -> drivingLicense = if (isChecked) "1" else "0" }


        CK_2.setOnCheckedChangeListener { _, isChecked ->
            registration = if (isChecked) "1" else "0"
        }


        CK_3.setOnCheckedChangeListener { _, isChecked ->
            idCardCopy = if (isChecked) "1" else "0"
        }


        CK_4.setOnCheckedChangeListener { _, isChecked ->
            notice = if (isChecked) "1" else "0"
        }

        CK_5.setOnCheckedChangeListener { _, isChecked ->
            attorneyPaper = if (isChecked) "1" else "0"
        }

        CK_6.setOnCheckedChangeListener { _, isChecked ->
            plateApplicationForm = if (isChecked) "1" else "0"
        }

        radio_hasCheck.setOnCheckedChangeListener { _, isChecked ->
            hasCheck = if (isChecked) "1" else "0"
        }

        radio_hasRecheck.setOnCheckedChangeListener { _, isChecked ->
            hasRecheck = if (isChecked) "1" else "0"
        }
        //车辆可拆
        mCanDetachableCB.setOnClickListener {
            detachable = "1"
            mCanDetachableCB.isChecked = true
            mCantDetachableCB.isChecked = false
        }
        //车辆不可拆
        mCantDetachableCB.setOnClickListener {
            detachable = "0"
            mCanDetachableCB.isChecked = false
            mCantDetachableCB.isChecked = true
        }

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }


    override fun onClick(v: View?) {
        //提交
        if (v == confirm_button) {
            //处理图片和视频
            upDataToHttp()


        }
    }

    private fun upDataToHttp() {
        //完成
        when {
//            TextUtils.isEmpty(checkRemark.text.toString()) -> {
//                Toast.makeText(this, "初检备注不能为空", Toast.LENGTH_LONG).show()
//            }
            TextUtils.isEmpty(recheckRemark.text.toString()) -> {
                Toast.makeText(this, "二验备注不能为空", Toast.LENGTH_LONG).show()
            }
            else -> {
                loading.show()
                val map: MutableMap<String, String> =
                    HashMap()
                map["carId"] = carId
                map["carOwner"] = carOwner.text.toString()
                map["carSign"] = carSignstr
                map["ownerPlate"] = carPlate.toString()
                map["drivingLicense"] = drivingLicense
                map["registration"] = registration
                map["idCardCopy"] = idCardCopy
                map["notice"] = notice
                map["attorneyPaper"] = attorneyPaper
                map["plateApplicationForm"] = plateApplicationForm
                map["hasCheck"] = hasCheck
                map["checkRemark"] = checkRemark.text.toString()
                map["hasRecheck"] = hasRecheck
                map["recheckRemark"] = recheckRemark.text.toString()
                map["detachable"] = detachable
                MyLog.d(map.handlerNull())
                //二验数据上传
                viewModel.addEntranceCar(token!!, map)
            }
        }


//        MyLog.d("上传文件图片地址:$newImagePath")
//        MyLog.d("上传文件的地址:$newVideoPaths")
//        MyLog.d("carId:$carId")
//        UpLoadFile.upLoadFile(newImagePath,newVideoPaths,carId,object:Callback{
//            override fun onFailure(call: Call, e: IOException) {
//                MyLog.d("文件上传错误回调:${e.message}")
//                loading.dismiss()
//            }
//
//            override fun onResponse(call: Call, response: okhttp3.Response) {
//                loading.dismiss()
//                val result=response.body().toString()
//                MyLog.d("文件上传成功回调:$result")
////                val data= getGson().fromJson(result,ResultEntity::class.java)
////                MyLog.d("文件上传成功回调:${data.status}")
////                MyLog.d("文件上传成功回调:${data.data}")
////                MyLog.d("文件上传成功回调:${data.msg}")
//            }
//        })
    }

    private fun result() {

    }


    override fun onDestroy() {
        super.onDestroy()
        loading.dismiss()
    }
}